<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\RoomsResource;
use App\Models\AdminUser;
use App\Models\Consume;
use App\Models\Episode;
use App\Models\History;
use App\Models\Like;
use App\Models\Trace;
use App\Models\Type;
use App\Models\User;
use App\Models\Project;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;

class SaleController extends Controller
{
    public $sale_dep_id = 3;//销售部门ID

    public function list(Request $request)
    {
        $users = AdminUser::where('department_id', $this->sale_dep_id)->select('id', 'avatar', 'alias_name', 'department_id')->get();
        $users->each(function ($user) use ($users) {
            $user->avatar = !empty($user->avatar) ? env('APP_URL') . '/uploads/' . $user->avatar : '';
            $amountSum = Project::query()->where('user_id', $user->id)->sum('amount');
            $user->performance = is_numeric($amountSum) ? number_format($amountSum, 2) : 0;
        });
        $users_arr = $users->toArray();
        //将$users_arr按performance倒序排序
        usort($users_arr, function ($a, $b) {
            return (int)($b['performance']) - (int)($a['performance']);
        });

        return $this->success('success', $users_arr);
    }

}
